package family.yaongteam.connectlines;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class PaperManager {
	
	private View view;
	private List<Paper> paperList = new ArrayList<Paper>();
	int curPaperNum = 0;
	
	Point point = new Point(0,0);
	int curStartNum = 0;
	boolean isDone = false;

	public PaperManager(View view){
		this.view = view;		
		loadPaper();
	}
	
	protected void loadPaper(){
		Papers papers = new Papers(view);		
		paperList = papers.getPaperList();
	}	
	
	protected void initPaper(){
		point = new Point(0,0);
		curStartNum = 0;
		isDone = false;
	}
	
	public void draw(Canvas canvas){
		
		canvas.drawColor(Color.LTGRAY);
		
		Paint paint = new Paint();
		paint.setStrokeWidth(10);
		
		// 현재 터치된 곳 선 긋기
		PointInfo pi = getStartPoint();
		paint.setColor(Color.WHITE);
		canvas.drawLine(pi.getX(), pi.getY(), point.x, point.y, paint);
		
		// 이미지 그리기, 포인트 그리기, 선 긋기
		getCurrentPaper().draw(canvas, paint, curStartNum, isDone);
	}
	
	public Paper getCurrentPaper(){
		return paperList.get(curPaperNum);
	}
	
	public PointInfo getStartPoint()
	{
		List<PointInfo> pointInfoList = getCurrentPaper().getPointInfoList();
		return pointInfoList.get(curStartNum);
	}
	
	public PointInfo getNextPoint()
	{
		List<PointInfo> pointInfoList = getCurrentPaper().getPointInfoList();
		int idx = curStartNum+1;
		if(idx > pointInfoList.size()-1) return null;
		return pointInfoList.get(idx);
	}

	public boolean touch(MotionEvent event){
		List<PointInfo> pointInfoList = getCurrentPaper().getPointInfoList();
		if(curStartNum >= pointInfoList.size()-1) return true;
		
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			point.x = (int)event.getX();
			point.y = (int)event.getY();
			view.invalidate();
			return true;
		}
		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			point.x = (int)event.getX();
			point.y = (int)event.getY();
			view.invalidate();
			return true;
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			// 
			PointInfo pi = getNextPoint();
			float disX = Math.abs(pi.getX()-event.getX());
			float disY = Math.abs(pi.getY()-event.getY());
			
			if(disX < 15 && disY < 15)
			{
				point.x = (int)pi.getX();
				point.y = (int)pi.getY();
				
				curStartNum++;
				Log.d("curStartNum",Integer.toString(curStartNum));
				if(curStartNum == pointInfoList.size()-1)
				{
		            Toast.makeText(view.getContext(), "You did it", Toast.LENGTH_SHORT).show();
		            isDone = true;
				}
			}
			else
			{
				point.x = (int)getStartPoint().getX();
				point.y = (int)getStartPoint().getY();	
			}
			view.invalidate();
			return true;
		}
		return false;		
	}
	
	public void goNext(){
		if(curPaperNum >= paperList.size()-1){
			curPaperNum = paperList.size()-1;
			return;
		}
		curPaperNum = curPaperNum + 1;
		initPaper();
		view.invalidate();
	}
	
	public void goPre(){
		if(curPaperNum <= 0){
			curPaperNum = 0;
			return;
		}
		curPaperNum = curPaperNum - 1;
		initPaper();
		view.invalidate();
	}
	/*
	public PointInfo getCurrentPoint()
	{
		List<PointInfo> pointInfoList = getCurrentPaper().getPointInfoList();
		for(PointInfo pi : pointInfoList)
		{
			if(pi.isUse() == false)
			{
				return pi;
			}
		}
		return null;
	}
	*/	
}
